//6. State Institutions: Judiciary and Law Enforcement

clear all
set more off
pause on
set trace off
cap log close
set matsize 5000

loc dir "/Users/mateo/Desktop/"
	
	cd "/Users/mateo/Desktop/"
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
	gen post=(Año>1995)
	gen any_titleXpost=any_title*post
	gen any_title2=any_title
	gen post2=post
	egen total_violence=rowtotal( sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)

	** Controls X post
	loc controlsXpost
	loc i
	
	foreach control of loc controls {
	
		loc i c.`control'##post
		
		loc controlsXpost `controlsXpost' `i' 

	}
	
	** Controls X year
	loc controlsXyear
	loc x
	
	foreach control of loc controls {
	
		loc x c.`control'##c.Año
		
		loc controlsXyear `controlsXyear' `x' 

	}
	
	
	di in red "`controlsXyear'"
		
	foreach x in 1995 {
	
		preserve
		
			keep if Año<=`x'
						
			collapse (mean)  spatlagguerrsum100_100, by(codmpio)
			
			ren (spatlagguerrsum100_100)=_`x'
			
			tempfile neighbor
			save `neighbor', replace
		
		
		restore
		
		merge m:1 codmpio using `neighbor'
		drop _m
	
	
	}
	
	
	* Local state capacity  (Tax office, fire stations, culture houses, libraries, schools, post offices, telecom, notary, & public instrument office)
	
	
	* State capacity variables: pubinst_office tax_office total_pub_inst total_pub_emp fire_stations jails libraries schools health_post health_center hospital agbank_office post_office telecom notary courts police_posts police_inspection national_pubemp muni_pubemp
	
	
	egen health_inst=rowtotal(health_post health_center hospital)
	
	* National state capacity (Police, courts, public hospitals, & (partially) agricultural banks)
	
		egen national_state_capacity=rowtotal( courts police_posts police_inspection  hospital  agbank_office ) 
		* hospital  agbank_office   
	
		egen local_state_capacity=rowtotal(pubinst_office tax_office health_post culture_houses health_center fire_stations  libraries schools     post_office telecom notary  jails)
		
		** Law enforcement
		egen police=rowtotal(police_inspection police_posts)
		replace police=. if (police_inspection==. & police_posts==.)
		lab var police "\# Police posts \& inspections"
		lab var courts "Courts"

	** Judiciary
	
	estimates clear
		
	loc indepvars
	loc allvars
	
	foreach y of varlist sumattacks_paramilitary sumattacks_public  sumattacks_guerrilla {
	
		foreach var of varlist  courts    {
		
			cap gen as_`y'=asinh(`y')
			
			preserve
				
					eststo: qui  xi: areg  as_`y'  any_title##c.`var'##post i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
					
					estadd 	 loc time_FE "Yes"
					estadd 	 loc loc_FE "Yes"	
					estadd 	 loc timetrends "No"	
					estadd 	 loc controls "Yes"	
					estadd 	 loc controlsXpost "Yes"	
					estadd 	 loc DepartmentXyear "Yes"
					estadd	 loc region_fe "No"
					estadd 	 loc cluster = e(N_clust) 	

					sum as_`y'   if  e(sample)==1
					estadd scalar depvarmean=r(mean)
					
					loc indepvars `indepvars' 1.any_title#1.post#c.`var' 1.post#c.`var'   1.any_title#1.post
					loc allvars `allvars' any_title##c.`var'##post 
							
			restore	
													
		}
			
		
	}
	
	foreach y of varlist sumattacks_paramilitary sumattacks_public  sumattacks_guerrilla {
	
		foreach var of varlist  courts    {
		
			cap gen as_`y'=asinh(`y')
			
			preserve
				
					eststo: qui  xi: areg  as_`y'  any_title##c.`var'##post i.Año c.Año##i.coddepto  `controlsXpost' if gpacifica==1, r cluster(codmpio) absorb(codmpio)
					
					estadd 	 loc time_FE "Yes"
					estadd 	 loc loc_FE "Yes"	
					estadd 	 loc timetrends "No"	
					estadd 	 loc controls "Yes"	
					estadd 	 loc controlsXpost "Yes"	
					estadd 	 loc DepartmentXyear "Yes"
					estadd	 loc region_fe "No"
					estadd 	 loc cluster = e(N_clust) 	

					sum as_`y'   if  e(sample)==1
					estadd scalar depvarmean=r(mean)
					
					loc indepvars `indepvars' 1.any_title#1.post#c.`var' 1.post#c.`var'   1.any_title#1.post
					loc allvars `allvars' any_title##c.`var'##post 
							
			restore	
													
		}
			
		
	}
			#d ;
		esttab using "/Users/mateo/Desktop/Table6.tex", keep(`indepvars') order(`indepvars') replace se b(%9.3f) se(%9.3f) ar2(%9.3f) ar2  eqlabels(none) f  nodep nobaselevels  nonum noobs  label gaps nolines collabels(, none) star(* 0.10 ** 0.05 *** 0.01) 
		mtitles("\specialcell{[1]}" 
				"\specialcell{[2]}" 
				"\specialcell{[3]}"  
				"\specialcell{[4]}" 
				"\specialcell{[5]}" 
				"\specialcell{[6]}" 
				"\specialcell{[7]}" 
				"\specialcell{[8]}" 

				) 
		varlabels(1.any_title#1.post#c.total_pub_inst   		"\# Total Public Institutions $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.total_pub_emp			"\# $\text{Total Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				   1.any_title#1.post#c.national_pubemp			"\# $\text{National Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.muni_pubemp				"\# $\text{Municipal Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.schools					"\# Schools  $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.health_inst				"\# Health Institutions $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.courts					"\# National Courts $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.police_posts				"\# Police posts $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.police_inspection 		"\# National police inspections $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.jails					"\# Jails $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.libraries				"\# Libraries $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.tax_office				"\# Tax Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.agbank_office			"\# Agriculture Bank Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.post_office				"\# Post Offices Jails $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.telecom					"\# Telecom Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.notary					"\# Notary Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.fire_stations			"\# Fire Stations $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.national_state_capacity	"\# National-level state agencies $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.local_state_capacity		"\# Local-level state agencies $\times$ Any title $\times$ Post"
				  1.post#c.courts								"\# National Courts $\times$ Post"  
				  1.any_title#1.post 							"Any title $\times$ Post" 
				  c.courts 										"\# National Courts"
				  )  
		refcat( 1.any_title#1.post#c.total_pub_emp "\hline \\ \textbf{Panel A: Public Employees}" 
				1.any_title#1.post#c.national_pubemp "\hline \\ \textbf{Panel A: Public Employees}" 
				1.any_title#1.post#c.national_state_capacity "\hline \\ \textbf{Panel B: Public Institutions}" 
				1.any_title#1.post#c.total_pub_inst "\hline \\ \textbf{Panel A: Public Institutions}" 
				1.any_title#1.post#c.total_pub_inst "\hline \\ \textbf{Panel A: Public Institutions}" 
				1.any_title#1.post#c.courts 		"\hline \\ \textbf{Panel A: Judiciary}"
				1.any_title#1.post#c.police			"\hline \\ \textbf{Panel B: Law Enforcement}"
			   , nolabel)
		mgroups("\specialcell{Police \& \\ Army Attacks}" "\specialcell{Paramilitary \\ Attacks}" "\specialcell{Guerrilla \\ Attacks}", pattern(1 1 1 1)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  
		nonotes stats(r2_a N cluster depvarmean, layout(@ @ @ @) 
		fmt(a3 a3 a3 a3 a3 ) labels("Adj. R-squared" "Observations" "Clusters \smallskip" "Dependent Variable Mean"))
		/*nonotes stats(r2_a N cluster time_FE  loc_FE controls controlsXpost  DepartmentXyear   depvarmean, layout(@ @ @ @ @ ) 
		fmt(a3 a3 a3 a3 a3 ) labels("Adj. R-squared" "Observations" "Clusters" "Year F.E."  "Municipality F.E." "Controls" "Controls $\times$ Post" "Department time trends \smallskip" "Dependent Variable Mean"   ))*/
		;
	#d cr
	
	
	** Law Enforcement 
	
	estimates clear
		
	loc indepvars
	loc allvars
	
	
	foreach y of varlist sumattacks_paramilitary sumattacks_public  sumattacks_guerrilla {
	
		foreach var of varlist  police    {
		
			cap gen as_`y'=asinh(`y')
			
			preserve
				
					eststo: qui  xi: areg  as_`y'  any_title##c.`var'##post i.Año c.Año##i.coddepto  `controlsXpost', r cluster(codmpio) absorb(codmpio)
					
					estadd 	 loc time_FE "Yes"
					estadd 	 loc loc_FE "Yes"	
					estadd 	 loc timetrends "No"	
					estadd 	 loc controls "Yes"	
					estadd 	 loc controlsXpost "Yes"	
					estadd 	 loc DepartmentXyear "Yes"
					estadd	 loc region_fe "No"
					estadd 	 loc cluster = e(N_clust) 	

					sum as_`y'   if  e(sample)==1
					estadd scalar depvarmean=r(mean)
					
					loc indepvars `indepvars' 1.any_title#1.post#c.`var' 1.post#c.`var'   1.any_title#1.post
					loc allvars `allvars' any_title##c.`var'##post 
							
			restore	
													
		}
			
		
	}
			
	

		foreach y of varlist sumattacks_paramilitary sumattacks_public  sumattacks_guerrilla {
	
		foreach var of varlist  police    {
		
			cap gen as_`y'=asinh(`y')
			
			preserve
				
					eststo: qui  xi: areg  as_`y'  any_title##c.`var'##post i.Año c.Año##i.coddepto  `controlsXpost' if gpacifica==1, r cluster(codmpio) absorb(codmpio)
					
					estadd 	 loc time_FE "Yes"
					estadd 	 loc loc_FE "Yes"	
					estadd 	 loc timetrends "No"	
					estadd 	 loc controls "Yes"	
					estadd 	 loc controlsXpost "Yes"	
					estadd 	 loc DepartmentXyear "Yes"
					estadd	 loc region_fe "No"
					estadd 	 loc cluster = e(N_clust) 	

					sum as_`y'   if  e(sample)==1
					estadd scalar depvarmean=r(mean)
					
					loc indepvars `indepvars' 1.any_title#1.post#c.`var' 1.post#c.`var'   1.any_title#1.post
					loc allvars `allvars' any_title##c.`var'##post 
							
			restore	
													
		}
			
		
	}
	
	#d ;
		esttab using "/Users/mateo/Desktop/Table6.tex", keep(`indepvars') order(`indepvars') append se b(%9.3f) se(%9.3f) ar2(%9.3f) ar2  eqlabels(none) f  nodep nobaselevels  nonum noobs  label gaps nolines collabels(, none) star(* 0.10 ** 0.05 *** 0.01) nomtitles
		varlabels(1.any_title#1.post#c.total_pub_inst   		"\# Total Public Institutions $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.total_pub_emp			"\# $\text{Total Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				   1.any_title#1.post#c.national_pubemp			"\# $\text{National Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.muni_pubemp				"\# $\text{Municipal Public Employees}_{\times 100}$ $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.schools					"\# Schools  $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.health_inst				"\# Health Institutions $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.courts					"\# Courts $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.police_posts				"\# Police posts $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.police_inspection 		"\# National police inspections $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.jails					"\# Jails $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.libraries				"\# Libraries $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.tax_office				"\# Tax Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.agbank_office			"\# Agriculture Bank Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.post_office				"\# Post Offices Jails $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.telecom					"\# Telecom Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.notary					"\# Notary Offices $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.fire_stations			"\# Fire Stations $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.national_state_capacity	"\# National-level state agencies $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.local_state_capacity		"\# Local-level state agencies $\times$ Any title $\times$ Post"
				  1.any_title#1.post#c.police		     		"\# Police posts \& inspections $\times$ Any title $\times$ Post"
				  1.post#c.police 								"\# Police posts \& inspections $\times$ Post" 
				  1.any_title#1.post 							"Any title $\times$ Post" 
				  c.police 										
				  )  
		refcat( 1.any_title#1.post#c.total_pub_emp "\hline \\ \textbf{Panel A: Public Employees}" 
				1.any_title#1.post#c.national_pubemp "\hline \\ \textbf{Panel A: Public Employees}" 
				1.any_title#1.post#c.national_state_capacity "\hline \\ \textbf{Panel B: Public Institutions}" 
				1.any_title#1.post#c.total_pub_inst "\hline \\ \textbf{Panel A: Public Institutions}" 
				1.any_title#1.post#c.total_pub_inst "\hline \\ \textbf{Panel A: Public Institutions}" 
				1.any_title#1.post#c.courts 		"\hline \\ \textbf{Panel A: Courts}"
				1.any_title#1.post#c.police			"\hline \\ \textbf{Panel B: Law Enforcement}"
			   , nolabel)
		/*mgroups("\specialcell{All \\ Attacks}" "\specialcell{Police \& \\ Army Attacks}" "\specialcell{Paramilitary \\ Attacks}" "\specialcell{Guerrilla \\ Attacks}", pattern(1 1 1 1)   prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) */ 
		nonotes stats(r2_a N cluster depvarmean, layout(@ @ @ @) 
		fmt(a3 a3 a3 a3 a3 ) labels("Adj. R-squared" "Observations" "Clusters \smallskip" "Dependent Variable Mean"))
		/*nonotes stats(r2_a N cluster time_FE  loc_FE controls controlsXpost  DepartmentXyear   depvarmean, layout(@ @ @ @ @ ) 
		fmt(a3 a3 a3 a3 a3 ) labels("Adj. R-squared" "Observations" "Clusters" "Year F.E."  "Municipality F.E." "Controls" "Controls $\times$ Post" "Department time trends \smallskip" "Dependent Variable Mean"   ))*/
		;
	#d cr
